package com.isunland.managesystem.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.isunland.managesystem.R;
import com.isunland.managesystem.entity.ScoreRank;
import com.isunland.managesystem.ui.ScoreRankListFragment;
import java.util.List;

/**
 * 对象功能:
 * 开发公司: 北京信普达系统工程有限公司
 * 开发人员: HuXiaoDan
 * 创建时间: 2017/4/17 16:37
 */
public class ScoreRankListAdapter extends ArrayAdapter<ScoreRank> {

  /**
   * ViewHolder class for layout.<br />
   * <br />
   * Auto-created on 2017-04-17 16:44:24 by Android Layout Finder
   * (http://www.buzzingandroid.com/tools/android-layout-finder)
   */
  private static class ViewHolder {
    public final LinearLayout rootView;
    public final TextView tvNumber;
    public final TextView tvPersonName;
    public final TextView tvRank;
    public final TextView tvOther;

    private ViewHolder(LinearLayout rootView, TextView tvNumber, TextView tvPersonName,
        TextView tvRank, TextView tvOther) {
      this.rootView = rootView;
      this.tvNumber = tvNumber;
      this.tvPersonName = tvPersonName;
      this.tvRank = tvRank;
      this.tvOther = tvOther;
    }

    public static ViewHolder create(LinearLayout rootView) {
      TextView tvNumber = (TextView) rootView.findViewById(R.id.tv_number);
      TextView tvPersonName = (TextView) rootView.findViewById(R.id.tv_personName);
      TextView tvRank = (TextView) rootView.findViewById(R.id.tv_rank);
      TextView tvOther = (TextView) rootView.findViewById(R.id.tv_other);
      return new ViewHolder(rootView, tvNumber, tvPersonName, tvRank, tvOther);
    }
  }

  @Override public View getView(int position, View convertView, ViewGroup parent) {
    final ViewHolder vh;
    if (convertView == null) {
      View view = mInflater.inflate(R.layout.adapter_score_rank, parent, false);
      vh = ViewHolder.create((LinearLayout) view);
      view.setTag(vh);
    } else {
      vh = (ViewHolder) convertView.getTag();
    }

    ScoreRank item = getItem(position);
    // TODOBind your data to the views here
    vh.tvNumber.setText((position + 1) + "");
    vh.tvPersonName.setText(item.getStaffName());

    //当月排名
    if (ScoreRankListFragment.MONTH.equalsIgnoreCase(mType)) {
      vh.tvRank.setText(item.getMonth());
      vh.tvOther.setVisibility(View.GONE);
    }
    //年度排名
    if (ScoreRankListFragment.YEAR.equalsIgnoreCase(mType)) {
      vh.tvRank.setText(item.getYearSumvalue());
      vh.tvOther.setVisibility(View.VISIBLE);
      if ("0".equalsIgnoreCase(item.getYearPunvalue())) {
        vh.tvOther.setText(
            "（" + item.getYearRewvalue() + "  " + item.getYearPunvalue() + "）");// 格式: (奖励分  -扣减分)
      } else {
        vh.tvOther.setText(
            "（" + item.getYearRewvalue() + "  -" + item.getYearPunvalue() + "）");// 格式: (奖励分  -扣减分)
      }
    }

    return vh.rootView;
  }

  private LayoutInflater mInflater;
  private String mType;

  // Constructors
  public ScoreRankListAdapter(Context context, List<ScoreRank> objects, String type) {
    super(context, 0, objects);
    this.mInflater = LayoutInflater.from(context);
    this.mType = type;
  }

  public ScoreRankListAdapter(Context context, ScoreRank[] objects) {
    super(context, 0, objects);
    this.mInflater = LayoutInflater.from(context);
  }
}

